<?php
namespace app\api\controller\wanlshop;

use app\common\controller\Api;
use fast\Arr;

/**
 * WanlShop 余额
 */
class MoneyLog extends Api
{
    protected $noNeedLogin = [];
    protected $noNeedRight = ['*'];
    
	public function transfer(){
        $mobile = $this->request->post('mobile');
        $money = $this->request->post('money');//转账的金额
        //查询手机号是否存在
        $touser = model('app\common\model\User')->where(array('mobile'=>$mobile))->find();//本用户
        if(empty($touser)||empty($mobile)){
            $this->error('转账手机号有误');
        }
        if($touser['id'] == $this->auth->id){
            $this->error('转账手机号不能是自己');
        }
        $user = model('app\common\model\User')->get($this->auth->id);//本用户
        if (!$user || $user['money'] < $money) {
            $this->error('余额不足');
        }
        $insertdata = [
            'user_id' => $user['id'],
            'amount'=>$money,
            'tuser_id'=>$touser['id']
        ];
        //新增余额转账记录
        $res = model('app\common\model\TransferOrder')->save($insertdata);
        $id = model('app\common\model\TransferOrder')->id;
        //转账人/被转账人余额变动记录
        model('app\common\model\MoneyLog')->create([ //本用户余额减少
            'user_id' => $user['id'],
            'before'=>$user['money'],
            'after'=>$user['money']-$money,
            'money' => -$money, // 操作金额
            'memo' => "余额转赠", // 备注
            'type' => 'transfer', // 类型
            'service_ids' => $id // 业务ID
        ]);

        model('app\common\model\MoneyLog')->create([ //被转账人余额增加记录
            'user_id' => $touser['id'],
            'before'=>$touser['money'],
            'after'=>$touser['money']+$money,
            'money' => $money, // 操作金额
            'memo' => "余额转赠", // 备注
            'type' => 'transfer', // 类型
            'service_ids' => $id // 业务ID
        ]);

        //转账人/被转账人余额变动
        $user->money = $user['money'] - $money;
        $user->save();

        $touser->money = $user['money'] + $money;
        $touser->save();
        $this->success('转账成功');
    }

	
}
